package com.fulihui.wgj.consumer.service.impl;

import com.alibaba.dubbo.config.annotation.Reference;
import com.fulihui.wgj.consumer.entity.WxUserInfoExp;
import com.fulihui.wgj.consumer.service.SubUserDatilService;
import com.fulihui.wgj.facade.dto.WxSubUserDetailDTO;
import com.fulihui.wgj.facade.request.subscribe.WxSubUserDetailRequest;
import com.fulihui.wgj.facade.request.subscribe.WxSubUserDetailUpdateRequest;
import com.fulihui.wgj.facade.service.subdetail.WxSubUserDetailService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * @author: LiuJie
 * @version: 2018/7/4 14:43
 * @description: 记录用户关注、渠道等情况
 */
@Service
@Slf4j
public class SubUserDatilServiceImpl implements SubUserDatilService {

    @Reference(version = "1.0.0")
    WxSubUserDetailService wxSubUserDetailService;

    private final String  UNSUBSCRIBE = "0";
    private final String  SUBSCRIBE = "1";
    @Override
    public void reocdeSubUserDatil(WxUserInfoExp record) {
        if(record == null || StringUtils.isBlank(record.getAppId())
                || StringUtils.isBlank(record.getOpenid())){
            return;
        }

        WxSubUserDetailDTO wxSubUserDetail = wxSubUserDetailService.queryByAppIdAndOpenid(record.getAppId(), record.getOpenid()).getData();
        if(wxSubUserDetail == null){

            String unionid = record.getUnionid();
            Long subscribeTime = record.getSubscribeTime();

            WxSubUserDetailRequest wxSubUserDetailRequest = new WxSubUserDetailRequest();

            wxSubUserDetailRequest.setAppId(record.getAppId());
            wxSubUserDetailRequest.setOpenid(record.getOpenid());
            wxSubUserDetailRequest.setSubscribeTime(new Date(subscribeTime));

            wxSubUserDetailRequest.setUnionid(unionid);
            wxSubUserDetailRequest.setSubscribeScene(record.getSubscribeScene());
            wxSubUserDetailRequest.setQrScene(record.getQrScene());
            wxSubUserDetailRequest.setSceneCode(record.getQrSceneStr());
            if(!wxSubUserDetailService.saveWxSubUserDetail(wxSubUserDetailRequest).isSuccess()){
                log.error("wxSubUserDetailDao!!insert fail;");
            }
        }else{
            //取关又关注的情况，更新关注时间、来源
            WxSubUserDetailUpdateRequest wxSubUserDetailUpdateRequest = new WxSubUserDetailUpdateRequest();
            wxSubUserDetailUpdateRequest.setId(wxSubUserDetail.getId());

            wxSubUserDetailUpdateRequest.setSubscribeScene(record.getSubscribeScene());
            wxSubUserDetailUpdateRequest.setQrScene(record.getQrScene());
            wxSubUserDetailUpdateRequest.setSceneCode(record.getQrSceneStr());

            wxSubUserDetailUpdateRequest.setIsSubscribe(SUBSCRIBE);
            wxSubUserDetailUpdateRequest.setSubscribeTime(new Date());

            if(!wxSubUserDetailService.updateDetailById(wxSubUserDetailUpdateRequest).isSuccess()){
                log.error("wxSubUserDetailDao!!recordSubscribe!!update fail;");
            }
        }

    }

    @Override
    public void recordUnSubscribe(String appid,String openid) {
        if(StringUtils.isBlank(appid)
                || StringUtils.isBlank(openid)){
            return;
        }
        WxSubUserDetailDTO wxSubUserDetail = wxSubUserDetailService.queryByAppIdAndOpenid(appid, openid).getData();
        if(wxSubUserDetail == null){
            return;
        }
        WxSubUserDetailUpdateRequest wxSubUserDetailUpdateRequest = new WxSubUserDetailUpdateRequest();
        wxSubUserDetailUpdateRequest.setId(wxSubUserDetail.getId());
        wxSubUserDetailUpdateRequest.setIsSubscribe(UNSUBSCRIBE);
        wxSubUserDetailUpdateRequest.setUnsubscribeTime(new Date());

        if(!wxSubUserDetailService.updateDetailById(wxSubUserDetailUpdateRequest).isSuccess()){
            log.error("wxSubUserDetailDao!!recordUnSubscribe!!update fail;");
        }
    }
}
